package com.pieces
{
	import com.pieces.Coor;
	
	import mx.containers.Grid;
	import mx.containers.GridItem;
	import mx.containers.GridRow;
	import mx.containers.HBox;

	public class Piece
	{
		
		private var ay:Coor = new Coor(0);
		private var ax:Coor = new Coor(5);
		private var by:Coor = new Coor(1);
		private var bx:Coor = new Coor(5);
		private var cy:Coor = new Coor(2);
		private var cx:Coor = new Coor(5);
		private var dy:Coor = new Coor(3);
		private var dx:Coor = new Coor(5);
		
		private var alreadyPut:Boolean = false;;
		
		public function Piece()
		{
		}
		
		public function movePieceDown(grid:Grid):void{
			if(!alreadyPut){
				alreadyPut = true;
			} else {
				
			}
			
			moveCellDown(grid, dx, dy);
			moveCellDown(grid, cx, cy);
			moveCellDown(grid, bx, by);
			moveCellDown(grid, ax, ay);

			inc(dy);
			inc(cy);
			inc(by);
			inc(ay);
			
//			inc(a1);
//			inc(b1);
//			inc(c1);
//			inc(d1);
			
		}
		
		public function left(grid:Grid):Boolean{
			var can:Boolean = canMoveLeft(grid, dx, dy);
			can = can && canMoveLeft(grid, cx, cy);
			can = can && canMoveLeft(grid, bx, by);
			can = can && canMoveLeft(grid, ax, ay);
			
			if(!can){
				return false;
			}
			
			moveCellHorizontally(grid, dx, dy, -1);
			moveCellHorizontally(grid, cx, cy, -1);
			moveCellHorizontally(grid, bx, by, -1);
			moveCellHorizontally(grid, ax, ay, -1);
		
			add(dx, -1);
			add(cx, -1);
			add(bx, -1);
			add(ax, -1);
			
			return true;
		}
		
		public function canMoveLeft(grid:Grid, x:Coor, y:Coor):Boolean{
			var result = false;
			
			if(x.value - 1 >= 0) {
				return true;
			}
			
			return result;
		}
		
		public function canMoveRight(grid:Grid, x:Coor, y:Coor):Boolean{
			var result = false;
			
			if(x.value + 1 < TetrisFlex.COLS) {
				return true;
			}
			
			return result;
		}
		public function right(grid:Grid):Boolean{
			var can:Boolean = canMoveRight(grid, dx, dy);
			can = can && canMoveRight(grid, cx, cy);
			can = can && canMoveRight(grid, bx, by);
			can = can && canMoveRight(grid, ax, ay);
			
			if(!can){
				return false;
			}
			moveCellHorizontally(grid, dx, dy, 1);
			moveCellHorizontally(grid, cx, cy, 1);
			moveCellHorizontally(grid, bx, by, 1);
			moveCellHorizontally(grid, ax, ay, 1);
			
			add(dx, 1);
			add(cx, 1);
			add(bx, 1);
			add(ax, 1);	
			
			return true;
		}
		
		public function inc(i:Coor):void{
			if(i.value + 1 < TetrisFlex.ROWS) {
				i.inc();
			} else {
				i.value = 0;
			}
		}
		
		public function add(i:Coor, value:int):void{
			i.add(value);
			
		}
		
		public function canMoveCellDown(grid:Grid, x:Coor, y:Coor):Boolean{
			return null;
		}
		
		public function moveCellDown(grid:Grid, x:Coor, y:Coor):Boolean{
			
			//canMoveCellDown(grid:Grid, x:Coor, y:Coor)
			
			var box:HBox = getCell(grid, x.value, y.value);
			box.setStyle("backgroundColor", "#FF0000");
			
			if(y.value + 1 < TetrisFlex.ROWS) {
				var box2:HBox = getCell(grid, x.value, y.value+1);
				box2.setStyle("backgroundColor", "#000000");
			} else {
				var box2:HBox = getCell(grid, x.value, 0);
				box2.setStyle("backgroundColor", "#000000");
			}
			
			
			trace("move cell down " + x.value + " " + y.value);
			trace("box " + box.getStyle("backgroundColor"));
			trace("box2 " + box2.getStyle("backgroundColor"));
			
			return null;
		}
		
		public function getCell(grid:Grid, x:int, y:int):HBox{
			var row:GridRow = GridRow(grid.getChildAt(y));
			var item:GridItem = GridItem(row.getChildAt(x));
			return HBox(item.getChildAt(0));
		}
		
		public function moveCellHorizontally(grid:Grid, x:Coor, y:Coor, value:int):void{
			
				var box:HBox = getCell(grid, x.value, y.value);
				box.setStyle("backgroundColor", "#FF0000");
				var box2:HBox = getCell(grid, x.value + value, y.value);
				box2.setStyle("backgroundColor", "#000000");
			
		}
	}
}